﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PstBakup2013Server
{
    internal class RequestParser
    {
        internal struct Request
        {
            internal string Command { get; set; }
            internal string Parameter { get; set; }
        }

        internal Request ParseRequest(string strRequest)
        {
            Request request = new Request();

            request.Command = this.GetCommand(strRequest);
            request.Parameter = this.GetParameter(strRequest);

            return request;
        }

        /// <summary>
        /// Cuts the string where there is a colons' character, and returns the first part.
        /// </summary>
        /// <param name="request">The full request</param>
        /// <returns>The left part of the String if there is a colons' character, the full String otherwise</returns>
        private string GetCommand(string request)
        {
            if (request == null)
                return String.Empty;

            int index = request.IndexOf(':');

            return (index == -1 ? request : request.Substring(0, index));
        }

        /// <summary>
        /// Cuts the string where there is a colons' character, and returns the second part.
        /// </summary>
        /// <param name="request">The full request</param>
        /// <returns>The right part of the String if there is a colons' character, String.Empty otherwise</returns>
        private string GetParameter(string request)
        {
            if (request == null)
                return String.Empty;

            int index = request.IndexOf(':');

            return (index == -1 ? String.Empty : request.Substring(index + 1));
        }
    }
}
